package com.example.pan.server.common.stream.consumer.search;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.pan.core.utils.IdUtil;
import com.example.pan.server.common.stream.channel.PanChannels;
import com.example.pan.server.common.stream.event.search.UserSearchEvent;
import com.example.pan.server.modules.user.entity.RPanUserSearchHistory;
import com.example.pan.server.modules.user.service.IUserSearchHistoryService;
import com.example.pan.stream.core.AbstractConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: invictusQAQ
 * @Date: 2023-12-14-14:40
 * @Description: 用户搜索事件监听器
 */
@Component
public class UserSearchConsumer extends AbstractConsumer {

    @Autowired
    private IUserSearchHistoryService iUserSearchHistoryService;

    /**
     * 监听用户搜索事件，将其保存到用户搜索记录中
     *
     * @param message
     **/
    @StreamListener(PanChannels.USER_SEARCH_INPUT)
    public void saveSearchHistory(Message<UserSearchEvent> message) {
        if (isEmptyMessage(message)){
            return;
        }
        printLog(message);
        UserSearchEvent event = message.getPayload();
        RPanUserSearchHistory record = new RPanUserSearchHistory();
        Date date = new Date();
        record.setId(IdUtil.get());
        record.setUserId(event.getUserId());
        record.setSearchContent(event.getKeyword());
        record.setCreateTime(date);
        record.setUpdateTime(date);
        try {
            iUserSearchHistoryService.save(record);
        } catch (DuplicateKeyException e) {
            UpdateWrapper<RPanUserSearchHistory> updateWrapper = Wrappers.update();
            updateWrapper.eq("user_id", event.getUserId());
            updateWrapper.eq("search_content", event.getKeyword());
            updateWrapper.set("update_time", new Date());
            iUserSearchHistoryService.update(updateWrapper);
        }
    }
}
